package com.tony.pmp.server.controller;

import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.tony.pmp.common.response.BaseResponse;
import com.tony.pmp.common.response.StatusCode;
import com.tony.pmp.server.shiro.ShiroUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;

/**
 * Created with IntelliJ IDEA.
 *
 * @Title: SysLoginController
 * @Auther: 皮蛋布丁
 * @Date: 2021/07/11/14:13
 * @Description:
 */
@Controller
public class SysLoginController extends AbstractController {

    @Autowired
    private Producer producer;

    /**
    * @Description: login 登录
    * @Param: [username 用户名, password 密码, captcha 验证码]
    * @return: com.tony.pmp.common.response.BaseResponse
    * @Author: 皮蛋布丁
    * @Date: 2021/7/11 14:17
    */
    @RequestMapping(value = "/sys/login",method = RequestMethod.POST)
    @ResponseBody
    public BaseResponse login(String username,String password,String captcha) {
        log.info("用户名：{} 密码： {} 验证码：{}",username,password,captcha);
//        //校验验证码
//        String kaptcha=ShiroUtil.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
//        if (!kaptcha.equals(captcha)){
//            return new BaseResponse(StatusCode.InvalidCode);
//        }
        try {
            //region 提交登录
            Subject subject = SecurityUtils.getSubject(); //subject:提交的用户主体信息
            if (!subject.isAuthenticated()) {
                UsernamePasswordToken token = new UsernamePasswordToken(username, password);
                subject.login(token);
            }
            //endregion
        } catch (UnknownAccountException e) {
            return new BaseResponse(StatusCode.Fail.getCode(),e.getMessage());
        }catch (IncorrectCredentialsException e) {
            return new BaseResponse(StatusCode.AccountPasswordNotMatch);
        }catch (LockedAccountException e) {
            return new BaseResponse(StatusCode.AccountHasBeenLocked);
        }catch (AuthenticationException e) {
            return new BaseResponse(StatusCode.AccountValidateFail);
        }


        return new BaseResponse(StatusCode.Success);
    }

    /**
    * @Description: kaptcha 生成验证码
    * @Param: []
    * @return: void
    * @Author: 皮蛋布丁
    * @Date: 2021/7/12 21:01
    */
    @RequestMapping("captcha.jpg")
    public void captcha(HttpServletResponse response) throws Exception {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtil.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);

        System.out.println("验证码："+text);
    }

    /**
    * @Description: loginout 退出登录
    * @Param: []
    * @return: java.lang.String
    * @Author: 皮蛋布丁
    * @Date: 2021/7/12 21:26
    */
    @RequestMapping(value = "logout",method = RequestMethod.GET)
    public String loginout() {
        //销毁当前的shiro的用户session(会话)
        ShiroUtil.logout();
        return "redirect:/login.html";
    }
}
